package com.zj.common.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zj.common.util.ImageUploadUtil;
import com.zj.common.util.ResponseUtil;
import com.zj.common.util.StringMyUtil;
import com.zj.common.util.TokenUtil;
@Controller
@RequestMapping("downloadUtil")
public class DownloadUtil {

	    @RequestMapping(params ="imageUpload", method = RequestMethod.POST)
	    public void imageUpload(HttpServletRequest request, HttpServletResponse response) {
	        try {
	            ImageUploadUtil.ckeditor(request, response);
	        } catch (IllegalStateException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    
	    @RequestMapping(params ="down")
	    public void down(HttpServletRequest request, HttpServletResponse response) {
	        try {
	        	String wjid=request.getParameter("wjid");
	        	UploadController.downloadFile(request, wjid, response);
	        } catch (IllegalStateException e) {
	            e.printStackTrace();
	        }
	    }
	 
	@RequestMapping(params = "download", method = RequestMethod.GET)
	@ResponseBody
	public void download(HttpServletRequest req, @RequestParam("wjid") String wjid, HttpServletResponse resp) {
		String usertoken=req.getParameter("userToken");
		if(StringMyUtil.isEmpty(usertoken)){
			PrintWriter out = null;  
		    try {  
		        out = resp.getWriter();
			    String code = ResponseUtil.getJson(ResponseUtil.NOTOEKN_CODE, "鐢ㄦ埛鏈櫥褰�", "");
		        out.append(code);  
		       
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        if (out != null) {  
		            out.close();  
		        }  
		    }  
			
		}else{
			String id = TokenUtil.parseJWT(usertoken);
			if(id.equals(ResponseUtil.TOKENFALSE_CODE)){
				PrintWriter out = null;  
			    try {  
			        out = resp.getWriter();
			        String code = ResponseUtil.getJson(ResponseUtil.TOKENFALSE_CODE, "鐢ㄦ埛鏈櫥褰�", "");
			        out.append(code);  
			        
			    } catch (IOException e) {  
			        e.printStackTrace();  
			    } finally {  
			        if (out != null) {  
			            out.close();  
			        }  
			    }  
			}else{
				UploadController.downloadFile(req, wjid, resp);
			}
		}
		
	}
}
